package ws.cmpe451.group7.ourcity.webservice.multimedia;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.imgscalr.Scalr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cmpe451.group7.ourcity.entity.Multimedia;
import cmpe451.group7.ourcity.entity.MultimediaType;
import cmpe451.group7.ourcity.service.MultimediaService;

/**
 * web service created to serve images
 */
@Controller
public class PlaceImageServlet {

	@Autowired
	private MultimediaService multimediaService;

	/**
	 * Prints the content of the image with the given id to the output stream
	 * 
	 * This is used for both web & Android parts
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/webservice/getMultimedia")
	protected void getMultimedia(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		Map<String, String[]> parameters = request.getParameterMap();
		Set<String> keySet = parameters.keySet();

		try {
			long id = -1;
			int width = 0;
			int height = 0;

			for (String key : keySet) {
				switch (key) {
				case "id":
					String idText = parameters.get(key)[0];
					id = Long.parseLong(idText);
					break;
				case "width":
					String widthText = parameters.get(key)[0];
					width = Integer.parseInt(widthText);
					break;
				case "height":
					String heightText = parameters.get(key)[0];
					height = Integer.parseInt(heightText);
					break;
				default:
					throw new ws.cmpe451.group7.ourcity.webservice.multimedia.exception.PlaceImageServletIllegalArgumentException(
							"Illegal argument is found: " + key);
				}
			}

			if (id == -1) {
				throw new ws.cmpe451.group7.ourcity.webservice.multimedia.exception.PlaceImageServletIllegalArgumentException(
						"Image ID is not given");
			}

			Multimedia media = multimediaService.findByID(id);

			if (media != null) {
				// other types are not implemented yet
				if (media.getMultimediaType() != MultimediaType.IMAGE) {
					return;
				}

				InputStream is = new ByteArrayInputStream(media.getFile());
				BufferedImage image = ImageIO.read(is);

				if (width != 0 || height != 0) {
					image = Scalr.resize(image, width, height);
				}

				ServletOutputStream outputStream = response.getOutputStream();
				String type = media.getMimeType();
				response.setContentType(type);

				String extension = type.substring(type.indexOf("/") + 1);

				ImageIO.write(image, extension, outputStream);
				outputStream.close();
			}
		} catch (
				NumberFormatException
				| ws.cmpe451.group7.ourcity.webservice.multimedia.exception.PlaceImageServletIllegalArgumentException e) {
			e.printStackTrace();
		}
	}
}
